package com.hula.onion.book.server.spider.platforms.xinxindai;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.MD5Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sun.security.provider.MD5;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/30 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class XinXinDaiSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(3);

        map.put("username", form.getUserName());
        map.put("password", MD5Utils.MD5Encode(MD5Utils.MD5Encode(form.getPassword())));
        map.put("verifyCode", form.getTicket());

        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {
        try {
            AccountInfo accountInfo = new AccountInfo();

            Document dc = Jsoup.parse(html);

            Element element = dc.selectFirst("p.black.mt54.font14.text-center");

            String amount = element.text();

            amount = amount.substring(0, amount.length() - 1);

            //总资产
            accountInfo.setAmount(new BigDecimal(amount));

            //待收总收益
            String waitIntrest = dc.selectFirst("span.font-2096e9.font16.pl5.yh_a2").text();
            accountInfo.setWaitIntrest(new BigDecimal(waitIntrest));

            return new CommonResponse(accountInfo);

        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }
}
